package org.wang.web.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.wang.web.dao.SpringJDBCSuperDao;
import org.wang.web.model.Person;

public class MyServiceImpl implements MyService {
	
	private DataSource dataSource;
	
	private SpringJDBCSuperDao springJDBCSuperDao;
	
	private String name;

	@Override
	public String getName() {
		List<Person> list = springJDBCSuperDao.getPersonList();
		return list.toString();
	}

	@Override
	public List<String> getInfo() {
		List<String> list = new ArrayList<String>();
		list.add("guanyu");
		list.add("zhaoyun");
		list.add("caocao");
		return list;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String getCreate() {
		
		try {
			Connection connect = dataSource.getConnection();
			connect.setAutoCommit(false);
			PreparedStatement pst = connect.prepareStatement("SELECT userid,username,age FROM user_tab WHERE userid=?");
			//从1开始
			pst.setString(1, "user002");
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				this.setName(rs.getString("username"));
			}
			connect.commit();
			connect.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "create success";
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public SpringJDBCSuperDao getSpringJDBCSuperDao() {
		return springJDBCSuperDao;
	}

	public void setSpringJDBCSuperDao(SpringJDBCSuperDao springJDBCSuperDao) {
		this.springJDBCSuperDao = springJDBCSuperDao;
	}
	
}
